package com.dnui.seven2.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dnui.seven2.common.BaseContext;
import com.dnui.seven2.common.R;
import com.dnui.seven2.entity.Buymerch;
import com.dnui.seven2.service.BuymerchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/buyMerch")
public class BuyMerchController {

    @Autowired
    private BuymerchService buymerchService;

    /**
     * 添加购物车
     * @param buymerch
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody Buymerch buymerch){
        buymerch.setBuymerchTime(LocalDateTime.now());
        buymerchService.save(buymerch);


        return R.success("成功");
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<Buymerch>> list(){
        log.info("查看购物车...");


        List<Buymerch> buymerches = buymerchService.selectAll();

        return R.success(buymerches);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //SQL:delete from shopping_cart where user_id = ?

        LambdaQueryWrapper<Buymerch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Buymerch::getUserId,BaseContext.getCurrentId());

        buymerchService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }
}
